/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

/**
 *
 * @author Alex
 */
import java.io.*;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;


public class Model implements Serializable{
 private static Model model;
    private static Connection con;
public static Model getInstance() throws SQLException {
        if (model == null) {
            model = new Model();
        }

        model.conectar();
        return model;
    }
    /**
     * @return the con
     */
    public static Connection getCon() {
        return con;
    }
    private String jdbcDriver;
    private String jdbcUrl;
    private String user;
    private String password;
    private String database;
    private String puerto;

    public String getJdbcDriver() {
        return jdbcDriver;
    }

    public void setJdbcDriver(String jdbcDriver) {
        this.jdbcDriver = jdbcDriver;
    }

    public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDatabase() {
        return database;
    }

    public void setDatabase(String database) {
        this.database = database;
    }

        /**
     * @return the puerto
     */
    public String getPuerto() {
        return puerto;
    }

    /**
     * @param puerto the puerto to set
     */
    public void setPuerto(String puerto) {
        this.puerto = puerto;
    }

    public void conectar() throws SQLException {
        try {
            Class.forName(this.jdbcDriver);
        } catch (ClassNotFoundException ex) {
            throw new SQLException("Driver no cargado !!!"+ex.toString());
        }
        System.out.println("------------Conexion Driver--------- :"+this.getJdbcUrl()+this.getDatabase()+","+this.getUser()+","+this.getPassword());
        con = DriverManager.getConnection(this.getJdbcUrl()+this.getPuerto()+this.getDatabase(), this.getUser(), this.getPassword());
    }
    public boolean isConnect()
    {
        if(getCon()!=null)
        {
           return true;
        }else
        {
           return false;
        }
    }
    public void disconnect()
    {
        if(getCon()!=null)
        {
            try
            {
                getCon().close();
            }catch(SQLException ignore){}
            finally
            {
              con=null;
            }
        }
    }

    

//******************************************************METODOS PARA USUARIO************************



//******************************************************METODOS PARA USUARIO************************








//******************************************************METODOS PARA CLIENTE************************

 

 //******************************************************METODOS PARA CLIENTE************************




   //******************************************************METODOS PARA HABITACION************************






 //******************************************************METODOS PARA HABITACION************************





//******************************************************METODOS PARA PRODUCTOS Y SERVICIOS*********************





//******************************************************METODOS PARA PRODUCTOS Y SERVICIOS*********************

























   //******************************************************METODOS PARA HOSPEDAJE*********************
 public boolean existeHospedaje(String id,Date fecha ) throws SQLException{

		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=getCon().prepareStatement("SELECT COUNT(*) FROM hospedajes  WHERE id=?");
			pstmt.setString(1, id);
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return sw;
	}
//  private Hospedaje loadHospedaje(ResultSet rs) throws SQLException {
//       Hospedaje hos = new Hospedaje();
//
//      hos.setId(rs.getInt(1));
//
//      Cliente_DAO cd = new Cliente_DAO();
//      Cliente cli = (Cliente) cd.extrae(rs.getString(2));
//      hos.setCliente(cli);
//      hos.setFechaentrada(rs.getString(3));
//      hos.setCheckin(rs.getString(4));
//      hos.setFechasalida(rs.getString(5));
//
//      return hos;
//    }

//   public Hospedaje ExtraeHospedaje (String id) throws SQLException{
//                if (!isConnect())
//			throw new SQLException("no hay conexcion");
//                Hospedaje hos= new Hospedaje();
//		PreparedStatement pstmt=null;
//		ResultSet rs=null;
//
//		try{
//			pstmt=getCon().prepareStatement("SELECT * FROM hospedajes WHERE id=?");
//			pstmt.setString(1, id);
//			rs=pstmt.executeQuery();
//			if(rs.next()){
//			 hos=loadHospedaje(rs);
//
//                        }else{
//
//                        }
//		}finally{
//			if(rs!=null)
//				rs.close();
//
//			if(pstmt!=null)
//				pstmt.close();
//		}
//
//		return hos;
//     }
//

//******************************************************METODOS PARA HOSPEDAJE*********************



   //******************************************************METODOS PARA FACTURAS*********************
 public boolean existeFactura(String id) throws SQLException{

		boolean sw=true;
		PreparedStatement pstmt=null;
		ResultSet rs=null;

		try{
			pstmt=getCon().prepareStatement("SELECT COUNT(*) FROM factura  WHERE id=?");
			pstmt.setString(1, id);
			rs=pstmt.executeQuery();
			if(rs.next())
				if(rs.getString(1).equals("0"))
					sw=false;
		}finally{
			if(rs!=null)
				rs.close();

			if(pstmt!=null)
				pstmt.close();
		}

		return sw;
	}
//  private Factura loadFactura(ResultSet rs) throws SQLException {
//        Factura fac = new Factura();
//        fac.setId(rs.getInt(1));
//        fac.setId_hospedaje(rs.getInt(2));
//        fac.setEstado(rs.getString(3));
//        fac.setFechacreacion(rs.getString(4));
//
//        return fac;
//    }

//   public Factura ExtraeFactura (String id) throws SQLException{
//                if (!isConnect())
//			throw new SQLException("no hay conexcion");
//                Factura fac= new Factura();
//		PreparedStatement pstmt=null;
//		ResultSet rs=null;
//
//		try{
//			pstmt=getCon().prepareStatement("SELECT * FROM factura WHERE id=?");
//			pstmt.setString(1, id);
//			rs=pstmt.executeQuery();
//			if(rs.next()){
//			 fac=loadFactura(rs);
//
//                        }else{
//
//                        }
//		}finally{
//			if(rs!=null)
//				rs.close();
//
//			if(pstmt!=null)
//				pstmt.close();
//		}
//
//		return fac;
//     }


//        public List listarFacturas() throws SQLException {
//        if (!isConnect())
//		throw new SQLException("no hay conexcion");
//
//        PreparedStatement ps = null;
//        ResultSet rs = null;
//        Factura c = null;
//        List lista = new LinkedList();
//        try {
//            ps =(PreparedStatement)  getCon().prepareStatement("select * from factura ");
//            rs = ps.executeQuery();
//
//            while (rs.next()) {
//                c =loadFactura(rs);
//                lista.add(c);
//            }
//
//        } finally {
//            if (rs != null) {
//                rs.close();
//            }
//            if (ps != null) {
//                ps.close();
//            }
//        }
//        return lista;
//    }

//******************************************************METODOS PARA FACTURAS*********************



















/*

  public void agregarOficina(Oficina per)throws SQLException{
                if(existeOficina(per))
                    throw new SQLException("Ya existe esta oficina");
		PreparedStatement pstmt=null;

		try{

			pstmt=con.prepareStatement("INSERT INTO Oficina VALUES (?,?,?,?)");

			pstmt.setInt(1,per.getIdOficina());
			pstmt.setString(2,per.getDireccion());
			pstmt.setString(3,per.getDepartamento());
			pstmt.setString(4,per.getCiudad());

			pstmt.executeUpdate();
		}finally{
			if(pstmt!=null){
				pstmt.close();
				pstmt=null;
			}
		}

  }*/


/*
  public List listarPersonas() throws SQLException {
        if (!isConnect())
		throw new SQLException("no hay conexcion");

        PreparedStatement ps = null;
        ResultSet rs = null;
        Personas c = null;
        List lista = new LinkedList();
        try {
            ps =(PreparedStatement)  con.prepareStatement("select * from personas ");
            rs = ps.executeQuery();

            while (rs.next()) {
                c = Personas.loadPersonas(rs);
                lista.add(c);
            }

        } finally {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
        }
        return lista;*/
}
